﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Data;
using System.Data.SqlClient;

namespace TAMSUP
{
    /// <summary>
    /// Interaction logic for previewWindow.xaml
    /// </summary>
    public partial class detailReklame : Window
    {
        private String[] filename;
        private String id;
        private int count = 0;
        private int counter = 0;
        public detailReklame(String path, String id)
        {
            InitializeComponent();
            this.id = id;

            try
            {
                filename = Directory.GetFiles(path);

                count = filename.Count() - 1;

                BitmapImage bitmapSource2 = BitmapImageFromFile(filename[counter]);

                counter++;

                // Draw the Image
                image1.Source = bitmapSource2;
                image1.Stretch = Stretch.Uniform;
            } catch (Exception argex) {
                
            }

            labelID.Content = this.id;

            try
            {
                homeWindow.Con.Open();
                SqlDataAdapter da = new SqlDataAdapter("select Keterangan from Reklame where ID_Reklame = '" + id + "'", homeWindow.Con);
                DataSet ds = new DataSet();
                da.Fill(ds, "Daftar Path");
                textBox1.Text = ds.Tables["Daftar Path"].Rows[0].ItemArray.ElementAt(0).ToString();

                da = new SqlDataAdapter("select 'Rp ' + Convert(varchar(20),dbo.format_currency(PriceList),1) as [Price List] from Reklame where ID_Reklame = '" + id + "'", homeWindow.Con);
                DataSet dsp = new DataSet();
                da.Fill(dsp, "Price List");
                labelPrice.Content = dsp.Tables["Price List"].Rows[0].ItemArray.ElementAt(0).ToString();

                da = new SqlDataAdapter("select convert(varchar(10),tanggal_selesai,103) as tanggal_selesai from DaftarReklameDisewa where ID_Reklame = '" + id + "'", homeWindow.Con);
                DataSet dss = new DataSet();
                da.Fill(dss, "tanggal");
                if (dss.Tables["tanggal"].Rows.Count > 0)
                {
                    labelStatus.Content = "Isi";
                    labelTanggal.Content = dss.Tables["tanggal"].Rows[0].ItemArray.ElementAt(0).ToString();
                }
                else
                {
                    labelStatus.Content = "Kosong";
                    labelTanggal.Content = "Tidak Ada";
                }
                homeWindow.Con.Close();
            }
            catch (SqlException sqle)
            {
                MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
            }

        }

        private void image1_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private BitmapImage BitmapImageFromFile(string sFileName)
        {
            // Create source.
            BitmapImage bi = new BitmapImage();
            // Change properties within a BeginInit/EndInit block.
            bi.BeginInit();
            Uri mUri = new Uri("file://" + sFileName, UriKind.RelativeOrAbsolute);
            bi.UriSource = mUri;
            bi.EndInit();
            return bi;
        }

        private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine(count);
            if (counter >= count)
            {
                counter = 0;
            }

            BitmapImage bitmapSource2 = BitmapImageFromFile(filename[counter]);

            counter++;

            image1.Source = bitmapSource2;
            image1.Stretch = Stretch.Uniform;
        }

    }
}
